/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.inmp.opconf;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.Logger;

import com.sinodata.bsm.inmp.util.SystemContext;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangyazhou
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-6-25 下午2:36:07          wangyazhou        1.0         To create
 * </p>
 * 
 * @since 
 * @see     
 */
public class OpEventDelete {

    private static final Logger logger = Logger.getLogger(OpEventDelete.class);

    private static OpEventDelete instance;

    String opUrl = "";
    String apiKey = "";

    private OpEventDelete() {
        opUrl = (String) SystemContext.getProperty("op_url");
        apiKey = (String) SystemContext.getProperty("op_apikey");
    }

    public static OpEventDelete getInstance() {
        if (instance == null) {
            instance = new OpEventDelete();
        }
        return instance;
    }

    /**
     * 清除opmanager中的告警
     * @param entity
     *   /alarm/deleteAlarm       删除告警
     *   /alarm/clearAlarm        将告警状态改为已清除
     *   /alarm/acknowledgeAlarm  认领一个告警
     */
    public void deleteOpEvent(String entity) {
        if ("true".equals(((String) SystemContext.getProperty("op_event_clr")).trim())) {
            try {
                logger.info("[TIP]\t清除一条事件 entity=" + entity);

                HttpClient client = new HttpClient();
                String newUrl = opUrl + "/api/json/alarm/clearAlarm?apiKey=" + apiKey + "&entity=" + entity;
                HttpMethod method = new PostMethod(newUrl);
                client.executeMethod(method);
                method.releaseConnection();
            } catch (Exception e) {
                logger.error(e);
            }
        } else {
            return;
        }

    }
}
